Skip to content

LAB-1 dining philosophers solution#2

Open
quicklybly wants to merge 12 commits intoreview1from
master
Open

LAB-1 dining philosophers solution#2
quicklybly wants to merge 12 commits intoreview1from
master

Conversation

@quicklybly
Copy link

Описание решения доступно в README.md

Автор: Лысенко Артем Русланович

List<Thread> waiters = createAndStartWaiters(config.NUMBER_OF_WAITERS, orders, kitchen, statistic);

var startTime = System.nanoTime();
List<Thread> students = createAndStartStudents(config.NUMBER_OF_STUDENTS, spoons, statistic, orders);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лабу я конечно уже принял со скидкой, но тем не менее -- голые потоки как будто не стоит использовать. особенно когда у нас такой большой выбор экзекутор сервисов...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо
С экзекьютором и правда было бы красивее

private final Statistic statistic;
private final Spoon firstSpoon;
private final Spoon secondSpoon;
private final BlockingQueue<CompletableFuture<SoupOrderStatus>> orders;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На счет приоритета мы уже конечно пообщались, но если оставить это в стороне, то почему blocking? Почему не cuncurrent?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я хотел, чтобы consumer имел ожидание, тк в корутине или виртуальном треде, он может "заснуть", неблокирующая очередь сразу возвращает значение, значит цикл будет бежать без остановки

Вообще у меня давно в голове лежит знание, что неблокирующая очередь для producer/consumer не очень подходит, знание было взято на 2м курсе отсюда

baeldung примерно тоже самое пишет

On the other hand, if our consumer ends up waiting in a loop, we should probably choose a blocking queue as a better alternative.

Copy link
Author

@quicklybly quicklybly Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+Java заявляет, что ConcurrentLinkedQueue wait-free

This implementation employs an efficient "wait-free" algorithm based on one described in [Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms](http://www.cs.rochester.edu/u/michael/PODC96.html) by Maged M. Michael and Michael L. Scott.

но я этому не верю, в оригинальной статье, авторы называют свой алгоритм именно lock-free (возможен starvation, если тред постоянно проигрывает cas)

chatgpt, также считает

The ConcurrentLinkedQueue in Java is not strictly wait-free, but it is lock-free for most operations

try {
log.debug("Student {} took spoon with id: {}", id, firstSpoon.getId());
secondSpoon.lock();
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Жутковатый каскад try catch, в будущем лучше делать поаккуратнее. Из интереса можете глянуть соседние PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments